#测试登陆页
from selenium.webdriver.common.by import By

from common.Utils import BlogDriver

import time
class BlogLogin:
    url=""
    driver=""
    def __init__(self):
        self.url="http://8.137.19.140:9090/blog_login.html"
        self.driver=BlogDriver.driver
        self.driver.get(self.url)

    #检查登陆界面元素
    def Check_ele_Login(self):
        #检查用户名输入框
        self.driver.find_element(By.CSS_SELECTOR,"#username")
        #检查密码输入框
        self.driver.find_element(By.CSS_SELECTOR,"#password")
        #检查提交框
        self.driver.find_element(By.CSS_SELECTOR,"#submit")
        #检查系统名称
        txt=self.driver.find_element(By.CSS_SELECTOR,"body > div.nav > span").text
        assert txt=="我的博客系统"
        #检查主页框,检查写博客框
        self.driver.find_element(By.CSS_SELECTOR,"body > div.nav > a:nth-child(4)")
        self.driver.find_element(By.CSS_SELECTOR,"body > div.nav > a:nth-child(5)")


    def Clear_and_Alert(self):
        # 获取屏幕截图
        BlogDriver.getScreenShot()

        # 断言查看弹窗内容
        alert = self.driver.switch_to.alert
        text = alert.text
        print(text)
        # 弹窗点击确认
        alert.accept()

        self.driver.find_element(By.CSS_SELECTOR, "#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()

        # 测试登陆成功的测试用例
    def LoginSucTest(self):
        self.Check_ele_Login()
        #保险起见，先将输入框清空
        self.driver.find_element(By.CSS_SELECTOR,"#username").clear()
        self.driver.find_element(By.CSS_SELECTOR, "#password").clear()


        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("lisi")
        self.driver.find_element(By.CSS_SELECTOR,"#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR,"#submit").click()
        time.sleep(1)
        #判断是否成功登陆：看看是否能找到首页的用户名
        # name=self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.left > div > h3").text
        # print(name)

        #添加屏幕截图
        BlogDriver.getScreenShot()
        #返回登录页
        # self.driver.back()

    #测试登陆失败的测试用例
    def LoginFailTest(self):
        self.Check_ele_Login()

        #正确的用户名，错误的密码，然后出现弹窗
        self.driver.find_element(By.CSS_SELECTOR,"#username").send_keys("lisi")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("12345")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        self.Clear_and_Alert()
        self.Check_ele_Login()
        time.sleep(2)

        #错误用户名，正确密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("lisi2")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("123456")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        self.Clear_and_Alert()
        self.Check_ele_Login()
        time.sleep(2)

        #错误用户名，错误密码
        self.driver.find_element(By.CSS_SELECTOR, "#username").send_keys("lisi3")
        self.driver.find_element(By.CSS_SELECTOR, "#password").send_keys("1234565")
        self.driver.find_element(By.CSS_SELECTOR, "#submit").click()
        self.Clear_and_Alert()
        time.sleep(2)


login = BlogLogin()
# login.LoginFailTest()
# login.LoginSucTest()




